

def sumOfLeafNodes(root):
    # 如果节点为空，返回0
    if root is None:
        return 0

    # 如果当前节点是叶子节点，返回其值
    if root.left is None and root.right is None:
        return root.val

    # 递归计算左子树和右子树中叶节点的总和
    left_sum = sumOfLeafNodes(root.left)
    right_sum = sumOfLeafNodes(root.right)

    # 返回左右子树中叶节点总和的和
    return left_sum + right_sum


class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right


# 构建示例树
root = TreeNode(1)
root.left = TreeNode(2)
root.right = TreeNode(3)
root.left.left = TreeNode(4)
root.left.right = TreeNode(5)
root.right.left = TreeNode(6)
root.right.right = TreeNode(7)
root.left.right.right = TreeNode(8)

# 计算并输出叶节点的总和
leaf_sum = sumOfLeafNodes(root)
print("节点和:", leaf_sum)